package com.chinaunicom.auth.entity;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/3/8 9:58
 * Description: 系统用户实体类
 */

@Data
@TableName("sys_user")
public class SysUser implements UserDetails, Serializable {

    private String id;

    private String username;

    private String password;

    private Date createTime;

    private Date updateTime;

    private String mobile;

    @TableField(exist = false)
    private List<String> auths;

    // 封装权限
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 封装权限
        ArrayList<SimpleGrantedAuthority> authorities = new ArrayList<>();
        if (CollUtil.isNotEmpty(auths)) {
            auths.forEach(auth -> {
                authorities.add(new SimpleGrantedAuthority(auth));
            });
        }
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
